//
//  WFSaveTool.m
//  谷居
//
//  Created by dllo on 16/6/22.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFSaveTool.h"
#import "WFHandleURL.h"
@implementation WFSaveTool

+ (instancetype)shareSaveTool {
    static WFSaveTool *save = nil;
    static dispatch_once_t onceTojken;
    dispatch_once(&onceTojken, ^{
        
        save = [[WFSaveTool alloc] init];
    });
    return save;
    
}

- (NSMutableArray *)collectionArray {
    if (!_collectionArray) {
        self.collectionArray = [NSMutableArray new];
    }
    return _collectionArray;
}


#pragma mark - 归档

+ (BOOL)isModekSaved:(WFProjectModel *)model {
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
    NSString *path = [sandBox stringByAppendingPathComponent:@"collection.plist"];
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (array == nil) {
        return NO;
    } else {
        for (WFProjectModel *temp in array) {
            if (temp.id == model.id) {
                return YES;
            }
     }
    return NO;
    }
}

+ (void)removeModelFromDocument:(WFProjectModel *)model {
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
    NSString *path = [sandBox stringByAppendingPathComponent:@"collection.plist"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

    for (WFProjectModel *temp in arr) {
        if (model.id == temp.id) {
            [arr removeObject:temp];
            break;
        }
    }
    [NSKeyedArchiver archiveRootObject:arr toFile:path];
}

+ (void)addModelToDocument:(WFProjectModel *)model {
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
    NSString *path = [sandBox stringByAppendingPathComponent:@"collection.plist"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (arr == nil) {
        arr = [NSMutableArray array];
    }
    [arr addObject:model];
    [NSKeyedArchiver archiveRootObject:arr toFile:path];
}

+ (NSMutableArray *)getLikeArray {
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
    NSString *path = [str stringByAppendingPathComponent:@"collection.plist"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return arr;
}



+ (void)saveTxtToCachesWithArray:(NSMutableArray *)array {
    /** 创建路径. */
    [[StoreSandBox shareStoreSandBox] storeWithArray:array forKey:@"caseArray"];
    //    NSLog(@"%@")
    
    /** 获取图片网址. */
    NSMutableArray *photoArray = [NSMutableArray array];
    for (WFProjectModel *projectModel in array) {
        NSString *str = [WFHandleURL getPicURL:projectModel.coverPhoto];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
        UIImage *image = [UIImage imageWithData:data];
        [photoArray addObject:image];
    }
    [[StoreSandBox shareStoreSandBox] storeWithArray:photoArray forKey:@"photoArray"];
    NSLog(@"%@", [[StoreSandBox shareStoreSandBox] readWithArrayForKey:@"caseArray"]);
}

+ (void)savePicsToCachesWithImageArray:(NSMutableArray *)array {
    NSMutableArray *imageArray = [NSMutableArray array];
    for (WFUserimageModel *imageModel in array) {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageModel.small]];
        UIImage *image = [UIImage imageWithData:data];
        [imageArray addObject:image];
    }
    [[StoreSandBox shareStoreSandBox] storeWithArray:imageArray forKey:@"imageArray"];
}


#pragma mark - 打开数据库
- (void)open {
    
}

- (void)close {
    
}

- (void)create {
    
}

- (void)insert:(WFProjectModel *)model {
    
}
- (void)remove:(WFProjectModel *)model {
    
}

- (void)update {
    
}

@end
